#include "HardwareProfile.h"
// #include "string.h"
// #include "stdio.h"
// #include <ctype.h>
// #include "stdlib.h"
#define KEY1 0x01fe
#define KEY2 0x01fd
#define KEY3 0x01fb
#define KEY4 0x01f7
#define KEY5 0x01ef
#define KEY6 0x01df
#define KEY7 0x01bf
#define KEY8 0x017f
#define KEY9 0x00ff
unsigned char Key_Tx[6]=
{
	0xF1, 0x06, 0x01, 0xff, 0xF9, 0x8E
};
void KeyProc(void)
{
	unsigned int key,x;
	key=0;
	key=PORTC;
	if(RB4==1)
		key=key|0x0100;
	else
		key=key&0xfeff;
	if(key==0x01ff)
	{
		gFlags.bKey=0;
		return;
	}
	__delay_ms(20);
	x=0;
	x=PORTC;
	if(RB4==1)
		x=x|0x0100;
	else
		x=x&0xfeff;
	if(key!=x)
	{
		return;
	}
	if(gFlags.bKey)
		return;
	gFlags.bKey=1;
	switch(key)
	{
	case KEY1:
		Key_Tx[3]=0x01;
		Key_Tx[4]=GetVerify_Sum(Key_Tx,4);
		Uart1_PutChars(Key_Tx,6);
		BEEPIO=1;
		break;
	case KEY2:
		Key_Tx[3]=0x02;
		Key_Tx[4]=GetVerify_Sum(Key_Tx,4);
		Uart1_PutChars(Key_Tx,6);
		BEEPIO=1;
		break;
	case KEY3:
		Key_Tx[3]=0x03;
		Key_Tx[4]=GetVerify_Sum(Key_Tx,4);
		Uart1_PutChars(Key_Tx,6);
		BEEPIO=1;
		break;
	case KEY4:
		Key_Tx[3]=0x04;
		Key_Tx[4]=GetVerify_Sum(Key_Tx,4);
		Uart1_PutChars(Key_Tx,6);
		BEEPIO=1;
		break;
	case KEY5:
		Key_Tx[3]=0x05;
		Key_Tx[4]=GetVerify_Sum(Key_Tx,4);
		Uart1_PutChars(Key_Tx,6);
		BEEPIO=1;
		break;
	case KEY6:
		Key_Tx[3]=0x06;
		Key_Tx[4]=GetVerify_Sum(Key_Tx,4);
		Uart1_PutChars(Key_Tx,6);
		BEEPIO=1;
		break;
	case KEY7:
		Key_Tx[3]=0x07;
		Key_Tx[4]=GetVerify_Sum(Key_Tx,4);
		Uart1_PutChars(Key_Tx,6);
		BEEPIO=1;
		break;
	case KEY8:
		Key_Tx[3]=0x08;
		Key_Tx[4]=GetVerify_Sum(Key_Tx,4);
		BEEPIO=1;
		Uart1_PutChars(Key_Tx,6);
		break;
	case KEY9:
		Key_Tx[3]=0x09;
		Key_Tx[4]=GetVerify_Sum(Key_Tx,4);
		Uart1_PutChars(Key_Tx,6);
		BEEPIO=1;
		break;
	}
	if(BEEPIO)
	{
		__delay_20ms(10);
		BEEPIO=0;
	}
}
// unsigned char Calc(unsigned char rx)
// {
// 	unsigned char ret = c0_Data ^ 0x55;
// 	ret += rx;
// 	ret += 0x38;
// 	ret ^= 0x4a;
// 	ret ^= 0xfa;
// 	ret += 0x14;
// 	return ret;
// }
void Calc1(unsigned char rx)
{
	unsigned char dl, cl;//, t1, t2, t3, t4, t5, t6, t7, t8;
	dl = x1;
	dl += rx;
	//t1 = dl;
	cl = x2;
	cl += dl;
	//t2 = cl;
	dl = x3;
	dl += cl;
	//t3 = dl;
	cl = x4;
	cl += dl;
	//t4 = cl;
	dl = 0x38;
	dl += cl;
	//t5 = dl;
	cl = 0x4a;
	cl ^= dl;
	//t6 = cl;
	dl = 0xfa;
	dl ^= cl;
	//t7 = dl;
	cl = 0x14;
	cl += dl;
	//t8 = cl;
	dl = x3;
	x4 = dl;
	dl = x2;
	x3 = dl;
	dl = x1;
	x2 = dl;
	//cl = t8;
	x1 = cl;
}
